package com.gjintel.framework.dept.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.gjintel.framework.dept.dao.IDeptDao;
import com.gjintel.framework.dept.model.Dept;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 机构数据访问实现
 * @author fengya
 * @date 2015-7-31 上午10:50:13
 */
@Repository("deptDao")
public class DeptDaoImpl extends HibernateGenericDAO implements IDeptDao {
	@SuppressWarnings("unchecked")
	public List<Dept> findAll() {
		String hql = "from Dept d where d.flag=0";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	public void findAll(Page page, Dept dept, Sort sort,int loginUserType,String loginUserDeptUnit,String deptId) {
		StringBuffer hql = new StringBuffer("from Dept d where d.flag=0 and d.id!='1'");
		List<String> params = new ArrayList<String>(2);
		if(loginUserType == 0){
			hql.append("and d.type='3'");// and d.pUnit=?  and d.id!=? 
			//params.add(loginUserDeptUnit);
			//params.add(deptId);
		}
		if (dept != null) {
			if (!StringUtil.isEmpty(dept.getName())) {
				hql.append(" and d.name like ? ");
				params.add("%" + dept.getName().trim() + "%");
			}
			if (!StringUtil.isEmpty(dept.getPid())) {
				hql.append(" and d.pid=? ");
				params.add(dept.getPid().trim());
			}
		}
		this.findAsPage(hql.toString(), page, sort, "d", true, params.toArray());
	}
}
